import re
# .：通配符，代表任意字符（除\n以外），一个点一个字符
ret = re.findall('m...e','cat and mouse')
print(ret) # ['mouse']
# *：重复匹配，允许*之前的一个字符重复多次
ret = re.findall('ca*t','caaaaaat and mouse')
print(ret) # ['caaaaaat']
# ？：也是重复匹配，但是？之前的字符只能重复0次或者1次
ret = re.findall('ca?t','cat and mouse')
print(ret) # ['cat']
ret = re.findall('ca?t','caaaat and mouse')
print(ret) # [],无匹配
# +，重复匹配，至少重复一次，不可以是0次
ret = re.findall('ca+t','caaaat and mouse')
print(ret) # ['caaaat']
# {}，重复匹配，匹配次数可以自行设置，次数可以是一个数字或者一个范围
ret = re.findall('ca{4}t','caaaat and mouse')
print(ret) # 4次，['caaaat']
ret = re.findall('ca{1,4}t','caaat catd mouse')
print(ret) # 1-4次['caaat', 'cat']
# []定义匹配的字符范围
# [a-zA-Z0-9] 表示相应位置的字数要匹配英文和数字
ret = re.findall('[0-9]{1,5}','12 caaat catd 6 mouse')
print(ret) # ['12', '6']
ret = re.findall('[a-z]{1,3}','12 caaat catd 6 mouse')
print(ret) # ['caa', 'at', 'cat', 'd', 'mou', 'se']
# ^表示必须从字符串的起始位置开始匹配,不考虑后续字符串中是否存在
ret = re.findall('^c.t','cat cat and mouse')
print(ret) # ['cat']
# $,表示只从最后开始匹配
ret = re.findall('a.d$','cat cat and mouse')
print(ret) # []
ret = re.findall('m...e$','cat cat and mouse')
print(ret) # ['mouse']
# |两个模式进行或的匹配
ret = re.findall('cat|mouse','cat cat and mouse')
print(ret) # ['cat', 'cat', 'mouse']
# \转义字符,如果字符串中有特殊字符需要匹配
# . * ? + $ ^ [] {} | \ /
ret = re.findall('\^c.t','^cat cat and mouse')
print(ret) # ['^cat']
ret = re.findall('\[...\]','cat [cat] and mouse')
print(ret) # ['[cat]']